//
//  BHDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/31.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "BHDetViewController.h"

#import "BuyHDetView.h"
#import "PinJiaCell.h"
#import "PingJiaViewController.h"
#import "HXManageViewController.h"

#import "BuyHTwoView.h"
#import "BuyHThreeView.h"
@interface BHDetViewController ()<UITableViewDelegate,UITableViewDataSource>{
   UIView *headView;//table头视图
   BuyHDetView *det;
   BuyHTwoView *twoView;
   BuyHThreeView *threeView;
   UIView *view3;//户型view
   NSMutableDictionary *dic;//数据源
   NSMutableArray *datas;//
   UIView *views;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation BHDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setBGColor];
    [self showNav:@"楼盘详情"];
    dic = [NSMutableDictionary dictionary];
    datas = [NSMutableArray array];
    [self loadData];
    //[self loadUI];
    [self loadData2];
   
}

- (void)loadUI{
    //尾视图
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-60, ScreenWidth, 60)];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
   
    NSDictionary *houseop = [dic objectForKey:@"list"];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    line.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [footView addSubview:line];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(ScreenWidth/4*3, 10, ScreenWidth/4, 50);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"联系" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickTel) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:btn];
    
    UILabel *lab1 = [[UILabel alloc] init];
    lab1.text = [houseop objectForKey:@"title"];
    lab1.font = [UIFont systemFontOfSize:15.0];
    [footView addSubview:lab1];
    [lab1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footView.left).offset(16);
        make.top.equalTo(line.bottom).offset(8);
    }];
    
   UILabel *lab2 = [[UILabel alloc] init];
   lab2.text = [houseop objectForKey:@"address"];
   lab2.font = [UIFont systemFontOfSize:12.0];
   lab2.textColor = [UIColor darkGrayColor];
   [footView addSubview:lab2];
   [lab2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footView.left).offset(16);
        make.top.equalTo(lab1.bottom).offset(4);
   }];
   
   _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-60) style:UITableViewStylePlain];
   _tableView.delegate = self;
   _tableView.dataSource = self;
   [self.view addSubview:_tableView];
   
   headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 620)];
   _tableView.tableHeaderView = headView;
   
   
   //400+150+190+30
   //头视图
   det = [[[NSBundle mainBundle] loadNibNamed:@"BuyHDetView" owner:nil options:nil] lastObject];
   det.frame = CGRectMake(0, 0, ScreenWidth, 400);
   [headView addSubview:det];
   //赋值
   det.labTitle.text = [houseop objectForKey:@"title"];
   det.labPrice.text = [NSString stringWithFormat:@"%li元/m2起",[[houseop objectForKey:@"price"] integerValue]];
   NSString *labelling = [houseop objectForKey:@"labelling"];
   NSArray *arr = [labelling componentsSeparatedByString:@"|"];
   det.labSQG.text = arr[0];
   det.labSSQ.text = arr[1];
   det.labPlace.text = [houseop objectForKey:@"address"];
   det.labZX.text = [houseop objectForKey:@"fitmentfinish"];
   det.labYear.text = [NSString stringWithFormat:@"%li年",[[houseop objectForKey:@"quotationtime"] integerValue]];
   det.labCX.text = [houseop objectForKey:@"orientation"];
   det.labHState.text = [houseop objectForKey:@"type"];
   
   det.labSQG.layer.cornerRadius = 5.0;
   det.labSQG.layer.borderWidth = 1.0;
   det.labSQG.layer.borderColor = COLOR_RGB(17, 100, 253, 1).CGColor;
    
   det.labSSQ.layer.cornerRadius = 5.0;
   det.labSSQ.layer.borderWidth = 1.0;
   det.labSSQ.layer.borderColor = COLOR_RGB(17, 100, 253, 1).CGColor;
   
   [det.btnMore addTarget:self action:@selector(clickMore:) forControlEvents:UIControlEventTouchUpInside];
   
   //户型view
   view3 = [[UIView alloc] initWithFrame:CGRectMake(0, det.frame.origin.y+det.frame.size.height, ScreenWidth, 220)];
   view3.backgroundColor = [UIColor whiteColor];
   [headView addSubview:view3];
   //户型列表
   NSDictionary *hometype = [dic objectForKey:@"hometype"];
   NSInteger count = [[hometype objectForKey:@"count"] integerValue];
   
   UILabel *labLine0 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
   labLine0.backgroundColor = COLOR_RGB(242, 242, 242, 1);
   [view3 addSubview:labLine0];
   
   UILabel *labhx = [[UILabel alloc] init];
   labhx.font = [UIFont boldSystemFontOfSize:14.0];
   labhx.text = [NSString stringWithFormat:@"主力户型(%li)",count];
   [view3 addSubview:labhx];
   [labhx makeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(view3.left).offset(16);
      make.top.equalTo(labLine0.bottom).offset(8);
   }];
   
   UILabel *labLine1 = [[UILabel alloc] init];
   labLine1.backgroundColor = COLOR_RGB(242, 242, 242, 1);
   [view3 addSubview:labLine1];
   [labLine1 makeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(view3.left).offset(16);
      make.top.equalTo(labhx.bottom).offset(8);
      make.right.equalTo(view3.right);
      make.height.equalTo(1);
   }];
   
   UILabel *labLine2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 210, ScreenWidth, 10)];
   labLine2.backgroundColor = COLOR_RGB(242, 242, 242, 1);
   [view3 addSubview:labLine2];
   //户型图
   UIScrollView *sclView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 170)];
   sclView.contentSize = CGSizeMake(120*(count+1), 170);
   [view3 addSubview:sclView];
   
   NSMutableArray *value = [hometype objectForKey:@"value"];
   NSMutableArray *arr1 = [NSMutableArray array];
   NSMutableArray *arr2 = [NSMutableArray array];
   NSMutableArray *arr3 = [NSMutableArray array];
   for (NSDictionary *op in value) {
      NSString *area = [op objectForKey:@"area"];
      NSString *stra = [NSString stringWithFormat:@"套内%@m2户型图",area];
      NSString *type = [op objectForKey:@"ftype"];
      NSString *picname = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[op objectForKey:@"picimage"]];
      [arr1 addObject:picname];
      [arr2 addObject:stra];
      [arr3 addObject:type];
   }
   
   for (NSInteger i = 0; i<value.count; i++) {
      threeView = [[[NSBundle mainBundle] loadNibNamed:@"BuyHThreeView" owner:nil options:nil] lastObject];
      threeView.frame = CGRectMake(10*(i+1)+120*i, 0, 120, 170);
      [threeView.images sd_setImageWithURL:[NSURL URLWithString:arr1[i]] placeholderImage:nil];
      threeView.labTit.text = arr2[i];
      threeView.labDet.text = arr3[i];
      [sclView addSubview:threeView];
      
   }
   
}
//打电话
- (void)clickTel{
//   HXManageViewController *manager = [[HXManageViewController alloc] init];
//   [self.navigationController pushViewController:manager animated:YES];
   NSDictionary *op = [dic objectForKey:@"list"];
   NSString *phone = [NSString stringWithFormat:@"%li",[[op objectForKey:@"phone"] integerValue]];
   NSLog(@"phone:%@",phone);
   NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
   UIWebView *web = [[UIWebView alloc] init];
   [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
   [self.view addSubview:web];
   
}
//点击查看更多事件
- (void)clickMore:(UIButton *)btn{
   if (btn.tag == 10) {
      [btn setTitle:@"收起" forState:UIControlStateNormal];
      [btn setTitleColor:COLOR_RGB(17, 100, 253, 1) forState:UIControlStateNormal];
      [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
      
      headView.frame = CGRectMake(0, 0, ScreenWidth, 770);
      
      twoView = [[[NSBundle mainBundle] loadNibNamed:@"BuyHTwoView" owner:nil options:nil] lastObject];
      twoView.frame = CGRectMake(0, 400, ScreenWidth, 150);
      [headView addSubview:twoView];
      //550+190+30
      NSDictionary *op = [dic objectForKey:@"list"];
      twoView.labkfs.text = [op objectForKey:@"name"];
      twoView.labYear.text = [NSString stringWithFormat:@"%li年 (%@)",[[op objectForKey:@"otherstime"] integerValue],[op objectForKey:@"type"]];
      twoView.labJZStyle.text = [op objectForKey:@"cturetype"];
      twoView.labWY.text = [op objectForKey:@"layout"];
      twoView.labHouses.text = [NSString stringWithFormat:@"%li户",[[op objectForKey:@"profile"] integerValue]];
      twoView.labGreen.text = [NSString stringWithFormat:@"%li",[[op objectForKey:@"greeningrate"] integerValue]];
      
      view3.frame = CGRectMake(0, twoView.frame.origin.y+twoView.frame.size.height, ScreenWidth, 220);
      
      det.btnMore.tag = 20;
      [_tableView reloadData];
   }else{
      [btn setTitle:@"查看更多" forState:UIControlStateNormal];
      [btn setTitleColor:COLOR_RGB(17, 100, 253, 1) forState:UIControlStateNormal];
      [btn setImage:[UIImage imageNamed:@"lansexjt"] forState:UIControlStateNormal];
      
      headView.frame = CGRectMake(0, 0, ScreenWidth, 620);
      twoView.hidden = YES;
      view3.frame = CGRectMake(0, det.frame.origin.y+det.frame.size.height, ScreenWidth, 220);
      
      det.btnMore.tag = 10;
      [_tableView reloadData];
   }
   
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   static NSString *cellId = @"cell";
   PinJiaCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
   if (!cell) {
      cell = [[[NSBundle mainBundle] loadNibNamed:@"PinJiaCell" owner:nil options:nil] lastObject];
   }
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
   NSDictionary *op = datas[indexPath.row];
   cell.labName.text = [op objectForKey:@"nickname"];
   cell.labText.text = [op objectForKey:@"comment"];
   cell.labDate.text = [op objectForKey:@"commenttime"];
   NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[op objectForKey:@"avatar"]];
   [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"TIM图片20170810135854"]];
   
   return cell;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   return 100.0;
}
//头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   if (views) {
      return views;
   }
   views = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
   views.backgroundColor = [UIColor whiteColor];
   
   UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(16, 39, ScreenWidth-16, 0.5)];
   line.backgroundColor = COLOR_RGB(242, 242, 242, 1);
   [views addSubview:line];
   
   UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 100, 20)];
   NSString *str = [NSString stringWithFormat:@"店铺评价(%li)",datas.count];
   lab.text = str;
   lab.font = [UIFont boldSystemFontOfSize:14.0];
   [views addSubview:lab];
   
   return views;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   return 40.0;
}
//尾视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
   UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
   view.backgroundColor = [UIColor whiteColor];
   
   UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
   btn.frame = CGRectMake(0, 0, ScreenWidth, 40.0);
   [btn setTitle:@"查看全部评价" forState:UIControlStateNormal];
   [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
   btn.titleLabel.font = [UIFont systemFontOfSize:12.0];
   [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
   [view addSubview:btn];
   
   UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2+35, 14, 60, 12)];
   NSString *str = [NSString stringWithFormat:@"(%li)",datas.count];
   lab.text = str;
   lab.font = [UIFont systemFontOfSize:12.0];
   lab.textColor = [UIColor redColor];
   [view addSubview:lab];
   
   
   return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
   return 40.0;
}

- (void)click{
   PingJiaViewController *pingj = [[PingJiaViewController alloc] init];
   [self.navigationController pushViewController:pingj animated:YES];
   
}
//新房详情
- (void)loadData{
   [SVProgressHUD show];
   [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
   NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
   [parmas setObject:@"106" forKey:@"longitude"];
   [parmas setObject:@"29" forKey:@"latitude"];
   [parmas setObject:_hid forKey:@"id"];
   if ([user objectForKey:@"userid"]) {
      [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
   }
   
   NSLog(@"parmas:%@",parmas);
   AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
   NSString *url = [NSString stringWithFormat:@"%@/App/Bridalchamber/details",urlHead];
   [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
      NSLog(@"新房详情res:%@",responseObject);
      dic = [responseObject objectForKey:@"result"];
      
      [self loadUI];
      [_tableView reloadData];
      [SVProgressHUD dismiss];
   } failure:^(NSURLSessionDataTask *task, NSError *error) {
      NSLog(@"error:%@",error);
      [SVProgressHUD dismiss];
   }];
   
}
//买房评论
- (void)loadData2{
   NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
   [parmas setObject:_hid forKey:@"id"];
   [parmas setObject:@"1" forKey:@"p"];
   NSLog(@"parmas:%@",parmas);
   
   AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
   NSString *url = [NSString stringWithFormat:@"%@/App/Bridalchamber/appraise",urlHead];
   [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
      NSLog(@"买房评论res:%@",responseObject);
      NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
      if ([error integerValue] == 200) {
         datas = [[responseObject objectForKey:@"result"] objectForKey:@"comment"];
      }
      
      [_tableView reloadData];
   } failure:^(NSURLSessionDataTask *task, NSError *error) {
      NSLog(@"error:%@",error);
      
   }];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
